package net.brucecooper.mindmap.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.XMLParser;

public abstract class AbstractNodeDataStore implements NodeDataStore {
	List<NodeDataStoreListener> listeners = new ArrayList<NodeDataStoreListener>();
	
    public void addNodeDataStoreListener(NodeDataStoreListener l) {
    
    	listeners.add(l);
    }
    
    public void removeNodeDataStoreListener(NodeDataStoreListener l) {
    	listeners.remove(l);
    }

    protected void fireNodeChanged(int node) {
    	NodeDataStoreEvent e = null;
    	for (NodeDataStoreListener l : listeners) {
    		if (e == null) {
    			e = new NodeDataStoreEvent(this, node);
    		}
    		l.nodeChanged(e);
    	}
    }

    protected void fireNodeChanged(NodeDataStoreEvent e) {
        for (NodeDataStoreListener l : listeners) {
            l.nodeChanged(e);
        }
    }

    protected void fireNodeAdded(int parent, int child) {
    	NodeDataStoreEvent e = null;
    	for (NodeDataStoreListener l : listeners) {
    		if (e == null) {
    			e = new NodeDataStoreEvent(this, parent, child);
    		}
    		l.childAdded(e);
    	}
    }

    protected void fireNodeAdded(NodeDataStoreEvent e) {
        for (NodeDataStoreListener l : listeners) {
            l.childAdded(e);
        }
    }

    
    protected void fireNodeDeleted(NodeDataStoreEvent e) {
        for (NodeDataStoreListener l : listeners) {
            l.childDeleted(e);
        }
    }
    protected void fireNodeDeleted(int parent, int child) {
    	NodeDataStoreEvent e = null;
    	for (NodeDataStoreListener l : listeners) {
    		if (e == null) {
    			e = new NodeDataStoreEvent(this, parent, child);
    		}
    		l.childDeleted(e);
    	}
    }
    protected void fireModelChanged() {
    	NodeDataStoreEvent e = null;
    	for (NodeDataStoreListener l : listeners) {
    		if (e == null) {
    			e = new NodeDataStoreEvent(this, 0, -1);
    		}
    		l.modelChanged(e);
    	}
    }

    
    
    private Element createXMLRepresentation(Document doc, int id) {
    	Element newNod = doc.createElement("node");
        newNod.setAttribute("CREATED", Long.toString(System.currentTimeMillis()));
        newNod.setAttribute("MODIFIED", Long.toString(System.currentTimeMillis()));
        newNod.setAttribute("ID", "Link"+ id);
        newNod.setAttribute("TEXT", getNodeName(id));
        
    	List<Integer> children = getNodeChildren(id);
    	
    	if (children.size() > 0) {
    		for (int i: children) {
                Element child = createXMLRepresentation(doc, i);
                newNod.appendChild(child);
    		}
    	}
    	return newNod;
    }
    
    public String encodeModel() {
        Document doc = XMLParser.createDocument();
        Element root = doc.createElement("map");
        root.setAttribute("version", "0.8.1");
        doc.appendChild(root);

        
    	Element val = createXMLRepresentation(doc, ROOT_NODE);
    	root.appendChild(val);
    	return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + doc.toString();
    }
    
}
